//
//  WBNetworkManager+extension.swift
//  ZWWEIBO
//
//  Created by xzw on 2018/4/5.
//  Copyright © 2018年 xzw. All rights reserved.
//

import Foundation

extension WBNetworkManager{
    
    //获取当前登录用户及其所关注用户的最新微博
    //since_id:返回id比since_id大的微博，默认0
    //max_id:返回id小于或者等于max_id的微博，默认0
    func getstatusList(since_id: Int64 = 0, max_id: Int64 = 0,completed:@escaping (_ list:[[String:Any]]?,_ issuccess:Bool)->()){
        
        let urlStr = "https://api.weibo.com/2/statuses/home_timeline.json"
        let params = ["since_id":since_id,
                      "max_id":(max_id > 0) ? (max_id - 1) : 0 ]
        
        requestWithAccessToken(URLString: urlStr, parameters: params) { (response, issuccess) in
           
          //获取字典中key=statuses的数据
          let responsDic = response as? NSDictionary
          let statuses = responsDic?["statuses"] as? [[String : Any]]
            
          completed(statuses, issuccess) //回调结果
        }
    }
    
    //检测微博未读消息
    func getUnread(completed:@escaping(_ unreadCount:Int)->()){
        
        guard let uid = userModel.uid else {
            return
        }
        
        let urlStr = "https://rm.api.weibo.com/2/remind/unread_count.json"
        let params = ["uid":uid]
        
        requestWithAccessToken(URLString: urlStr, parameters: params) { (response, issuccess) in
            let dic = response as? [String : Any]
            let status = dic?["status"] as? Int
            completed(status ?? 0)
        }
    }
}

 //MARK: - 获取用户信息
extension WBNetworkManager {
    
    func getUerInfo(completed:@escaping (_ dic:[String:Any])->()){
        let url = "https://api.weibo.com/2/users/show.json"
        guard let uid = userModel.uid else{
            return
        }
        let dic = ["uid" : uid]
        requestWithAccessToken(URLString: url, parameters: dic) { (response, issuccess) in
            completed(response as? [String : Any] ?? [:])
        }
        
    }
    
}

 //MARK: - OAuth微博授权
extension WBNetworkManager{
    
    //获取access_token
    func getAcessToken(code: String,completed:@escaping (_ isSuccess:Bool)->()){
        
        let url = "https://api.weibo.com/oauth2/access_token"
        let params = ["client_id" : AppKey,
                      "client_secret" : AppSecret,
                      "grant_type" : "authorization_code",
                      "code" : code,
                      "redirect_uri" : Redirect_URL] as [String : Any]
        
        request(mothod: .POST, URLString: url, parameters: params) { (response, issuccess) in
            //字典转模型
            self.userModel.yy_modelSet(with: response as? [String:Any] ?? [:])
            
            //获取用户信息
            self.getUerInfo(completed: { (dic) in
                print(dic)
                //字典转模型
                self.userModel.yy_modelSet(with: dic)
                
                //保存模型
                self.userModel.saveUserModel()
                
                print(self.userModel)
                
                //完成回调
                completed(issuccess)
            })
            
        }
        
    }
}
